<template>
  <!-- 使用el-table -->
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <!-- 添加按钮 -->
          <el-button @click="openDrawer('add')" icon="el-icon-plus" size="small" type="primary" round>
  添加面经</el-button>
        </div>
        <!-- 表格 -->
        <el-table :data="list" style="width: 100%">
          <el-table-column prop="stem" label="标题" width="180">
          </el-table-column>
          <el-table-column prop="creator" label="作者" width="180">
          </el-table-column>
          <el-table-column prop="likeCount" label="点赞"> </el-table-column>
          <el-table-column prop="views" label="浏览数"> </el-table-column>
          <el-table-column prop="createdAt" label="更新时间"> </el-table-column>
          <el-table-column label="操作" width="120px">
  <template #default="{ row }">
    <div class="actions">
      <i class="el-icon-view" @click="openDrawer('preview', row.id, )"></i>
      <i class="el-icon-edit-outline" @click="openDrawer('edit', row.id)"></i>
      <i class="el-icon-delete" @click="del(row.id)"></i>
    </div>
  </template>
</el-table-column>
        </el-table>
        <!-- 抽屉 -->
        <el-drawer
  :visible.sync="isShowDrawer"
  :before-close="handleClose"
  :title="type"
  direction="rtl"
  size="50%"
>
  <el-form ref="form" label-width="80px" :model="form" :rules="rules" v-if="type!=='preview'">
  <el-form-item label="标题" prop="stem">
    <el-input v-model="form.stem" placeholder="输入面经标题"></el-input>
  </el-form-item>
  <el-form-item label="内容" prop="content">
    <quill-editor v-model="form.content"></quill-editor>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submit">确认</el-button>
    <el-button>取消</el-button>
  </el-form-item>
</el-form>
<div v-else>
  <!-- 预览 -->
  <h3>{{form.stem}}</h3>
  <p v-html="form.content"></p>
</div>
</el-drawer>
<!-- 分页 -->
        <el-pagination
  background
  layout="prev, pager, next"
  :total="1000" @current-change="currentChange">
</el-pagination>
      </template>
    </el-card>
  </div>
</template>

<script>
import { getArticleList, addArticleList, removeArticleList, getArticleListDetail, updateArticleListDetail } from '@/api/article'
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
export default {
  components: {
    quillEditor
  },
  name: 'article-page',
  data () {
    return {
      current: 1,
      pageSize: 10,
      total: 0,
      list: [],
      // 控制打开drawer抽屉的类
      type: '',
      isShowDrawer: false,
      form: {
        stem: '',
        content: ''
      },
      rules: {
        // 非空校验
        stem: [{ required: true, message: '需要填写内容', trigger: ['blur', 'change'] }],
        content: [{ required: true, message: '需要填写内容', trigger: ['blur', 'change'] }]
      }
    }
  },
  created () {
    this.initData()
  },
  methods: {
    async initData () {
      try {
        // 初始化数据 获得数据
        const { data } = await getArticleList({
          current: this.current,
          pageSize: this.pageSize
        })
        console.log(data)
        this.list = data.rows
        this.total = data.total
      } catch (error) {
        console.log('article错误信息', error)
      }
    },
    currentChange (val) {
      // console.log(val)
      this.current = val
      // 再次渲染
      this.initData()
    },
    // 开抽屉
    async openDrawer (type, id) {
      this.type = type
      console.log(type, id)
      this.isShowDrawer = true
      // 1.添加功能 不需要回显
      // 2.修改和查询需要回显
      if (this.type !== 'add') {
        const { data } = await getArticleListDetail(id)
        console.log(data)
        this.form = { ...data }
      }
    },
    // 关抽屉
    handleClose () {
      // 无论哪种清空表单
      this.form = { stem: '', content: '' }
      if (this.type !== 'preview') {
        // 请除表单数据
        this.$refs.form.resetFields()
      }
      this.isShowDrawer = false
    },
    // 点击el-drawer el-form添加功能
    async submit () {
      // console.log(1)
      // 校验
      await this.$refs.form.validate()
      await this.$refs.form.validateField()
      // 添加 add
      if (this.type === 'add') {
        // 添加
        const res = await addArticleList(this.form)
        console.log(res)
      }
      // 编辑功能 edit
      if (this.type === 'edit') {
        // 编辑功能 id stem content
        const { id, stem, content } = this.form
        const res = await updateArticleListDetail({ id, stem, content })
        console.log(res)
      }
      // 重新渲染 返回第一页
      this.current = 1
      this.initData()
      // 关闭抽屉
      this.handleClose()
    },
    del (id) {
      this.$confirm('此操作将永久删除该列表, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await removeArticleList(id)
        this.initData()

        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
